<?php
namespace app\index\controller;

use app\index\model\BidModel;
use app\index\model\CompanyModel;
use app\index\model\DeptModel;
use app\index\model\EmailInfoModel;
use app\index\model\EmailModel;
use app\index\model\EnginModel;
use app\index\model\FlowBillModel;
use app\index\model\FlowTaskModel;
use app\index\model\FlowTaskShenModel;
use app\index\model\LoginTokenModel;
use app\index\model\TaskModel;
use app\index\model\TodoModel;
use app\index\model\UserModel;
use app\index\model\UserRoleModel;
use app\index\service\MenuService;
use app\index\service\UserService;
use app\index\service\XingService;
use think\Db;
use think\Request;

class Index extends Common
{

    public function getuserinfo()
    {
        $data['id'] = $this->userinfo['id'];
        $data['realname'] = $this->userinfo['realname'];
        $data['deptname'] = DeptModel::get_name_by_pk($this->userinfo['deptid']);
        $data['comname'] = CompanyModel::where(['id'=>$this->userinfo['comid']])->value('name');
        $data['avatar'] = $this->userinfo['avatar'];
        $data['tel'] = $this->userinfo['tel'];

        $data['token_roletype'] = $this->userinfo['token_roletype'];
        $data['token_roletype_name'] = '';
        $rolearr = UserRoleModel::field('type as value')->where('id','in',$this->userinfo['roleid'])->group('type')->select()->toArray();
        foreach ($rolearr as &$item){
            $item['name'] = (new UserService())->get_user_role_type_name($item['value']);
            if($item['value'] == $this->userinfo['token_roletype']){
                $data['token_roletype_name'] = $item['name'];
            }
        }
        $data['rolearr'] = $rolearr;

        return outputJson(2000,lang('code_2000'),$data);
    }

    public function cutuserrole(Request $request)
    {
        $roletype = $request->param('roletype', 0, 'int');

        $roleidArr = UserRoleModel::where('id','in',$this->userinfo['roleid'])->group('type')->column('type');
        if(!in_array($roletype, $roleidArr)){
            return outputJson(4000, '操作失败');
        }

        LoginTokenModel::where(['id'=>$this->userinfo['token_id']])->update(['roletype'=>$roletype]);
        return outputJson(2000,lang('code_2000'));
    }


    public function getuserlock(Request $request)
    {
        $pass = $request->param('pass','','string');
        if($this->userinfo['pass'] == md5(md5($pass))){
            return outputJson(2000,lang('code_2000'));
        }
        return outputJson(4000,lang('code_4000'));
    }


    public function getusermenu(Request $request)
    {
        $pid = $request->param('pid',0, 'int');
        $data = (new MenuService())->user_menu($pid);
        return outputJson(2000,lang('code_2000'),$data);
    }

    public function getusermenumob(Request $request)
    {
        $data = (new MenuService())->user_menu_mob();
        return outputJson(2000,lang('code_2000'),$data);
    }



    public function getuserworker(){
        $data = [];

        $task = TaskModel::alias('t')
            ->field('t.id,t.name,t.type,bid.name as bidname,e.name as enginname')
            ->leftJoin('bid bid','bid.id=t.bidid')
            ->leftJoin('engin e','e.id=t.enginid')
            ->where(['t.deleted_user'=>0,'t.comid'=>$this->userinfo['comid']])
            ->where('t.status','in',[1,3])
            ->where(Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`t`.`zhier`)"))
            ->order('t.created_at DESC')->count();
        if($task > 0){
            $data['task'] = ['title' => '任务提醒', 'mess' => '您有（'.$task.'）个任务待办！', 'href' => 'taskzhi/index.php'];
        }

        $bill = FlowTaskModel::alias('task')
            ->field('bill.*,task.created_at as dt')
            ->join('flow_bill bill','bill.id=task.billid')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid'],'bill.isturn'=>1])
            ->where(['bill.status'=>0,'task.uid'=>$this->userinfo['id'],'task.state'=>1,'task.deleted_user'=>0,])
            ->order('task.created_at DESC')
            ->group('bill.id')->count();
        if($bill > 0){
            $data['bill'] = ['title' => '待办提醒', 'mess' => '您有（'.$bill.'）个待办事项！', 'href' => 'flow_hand/index.php'];
        }

        $todo = TodoModel::where(['uid'=>$this->userinfo['id'],'status'=>0])->count();
        if($todo > 0){
            $data['todo'] = ['title' => '消息提醒', 'mess' => '您有（'.$todo.'）条未读消息！', 'href' => 'todo/index.php'];
        }

        $email = EmailModel::where(['comid'=>$this->userinfo['comid'],'recid'=>$this->userinfo['id'],'reciddel'=>0,'isread'=>0])->count();
        if($email > 0){
            $data['email'] = ['title' => '邮件提醒', 'mess' => '您有（'.$email.'）条未读邮件！', 'href' => 'email/index.php'];
        }

        return outputJson(2000,lang('code_2000'),$data);
    }
    public function getuserworkermob(){
        $data = [];

        $taskinfo = TaskModel::where(['deleted_user'=>0,'comid'=>$this->userinfo['comid']])
            ->where('status','in',[1,3])
            ->where(Db::raw("FIND_IN_SET(".$this->userinfo['id'].",`zhier`)"))
            ->order('created_at DESC')->select()->toArray();
        $task['num'] = count($taskinfo);
        $task['note'] = '';
        $task['dt'] = '';
        if(isset($taskinfo[0])){
            $task['note'] .= (new XingService())->get_task_type_show($taskinfo[0]['type']);
            $task['note'] .= ' - '.$taskinfo[0]['name'];
            if($taskinfo[0]['type'] == 1){
                $enginname = BidModel::where(['id'=>$taskinfo[0]['bidid']])->value('name');
                $task['note'] .= ' - '.$enginname;
            }
            if($taskinfo[0]['type'] == 2){
                $enginname = EnginModel::where(['id'=>$taskinfo[0]['enginid']])->value('name');
                $task['note'] .= ' - '.$enginname;
            }
            $task['dt'] = $taskinfo[0]['created_at'];
        }
        $data['task'] = $task;

        $todoinfo = TodoModel::field('mess as note,tododt as dt')->where(['uid'=>$this->userinfo['id'],'status'=>0])->select()->toArray();
        $todo['num'] = count($todoinfo);
        $todo['note'] = isset($todoinfo[0]) ? $todoinfo[0]['note'] : '';
        $todo['dt'] = isset($todoinfo[0]) ? $todoinfo[0]['dt'] : '';
        $data['todo'] = $todo;

        $billinfo = FlowTaskModel::alias('task')
            ->field('bill.summary as note,task.created_at as dt')
            ->join('flow_bill bill','bill.id=task.billid')
            ->where(['bill.deleted_user'=>0,'bill.comid'=>$this->userinfo['comid'],'bill.isturn'=>1])
            ->where(['bill.status'=>0,'task.uid'=>$this->userinfo['id'],'task.state'=>1])
            ->order('task.created_at DESC')
            ->group('bill.id')->select()->toArray();
        $bill['num'] = count($billinfo);
        $bill['note'] = isset($billinfo[0]) ? $billinfo[0]['note'] : '';
        $bill['dt'] = isset($billinfo[0]) ? $billinfo[0]['dt'] : '';
        $data['bill'] = $bill;

        $emailinfo = EmailModel::field('id,uid,created_at as dt')
            ->where(['comid'=>$this->userinfo['comid'],'recid'=>$this->userinfo['id'],'reciddel'=>0,'isread'=>0])->select()->toArray();
        foreach ($emailinfo as &$item){
            $title = EmailInfoModel::where(['id'=>$item['id']])->value('title');
            $uidName = UserModel::get_realname_by_pk($item['uid']);
            $item['note'] = $title.' - '.$uidName;
        }
        $email['num'] = count($emailinfo);
        $email['note'] = isset($emailinfo[0]) ? $emailinfo[0]['note'] : '';
        $email['dt'] = isset($emailinfo[0]) ? $emailinfo[0]['dt'] : '';
        $data['email'] = $email;

        $data['allnum'] = $task['num'] + $todo['num'] + $bill['num'] + $email['num'];

        return outputJson(2000,lang('code_2000'),$data);
    }


}
